import 'package:flutter/material.dart';
import 'package:flutter_wisave/CentrePage/Device/item/BreakdownItem.dart';
import 'package:flutter_wisave/Tools/NetworkService.dart';
import 'package:flutter_wisave/HomePage/Class/NoticeClass.dart';
import '../../Tools/ToolsLocalStorage.dart';

class BreakdownDetailPage extends StatefulWidget {
  const BreakdownDetailPage({super.key});

  @override
  State<BreakdownDetailPage> createState() => _BreakdownDetailPageState();
}

class _BreakdownDetailPageState extends State<BreakdownDetailPage> {
  final NetworkService _networkService = NetworkService();
  List<dynamic> listData = [];
  get child => null;

  @override
  void initState() {
    super.initState();
    _networkService.setupInterceptors(context);
    _loadData();
  }

  Future<void> _loadData() async {
    try {
      final result = await _fetchData();
      if (result != null && result['rows'] != null) {
        setState(() {
          listData = (result['rows'] as List)
              .map((item) => (item as Map<String, dynamic>))
              .toList();
          print('data from BreakdownDetailPage');
        });
      } else {
        print('No data received from BreakdownDetailPage');
      }
    } catch (e) {
      print('Error in _loadData: $e');
    }
  }

  Future<Map<String, dynamic>?> _fetchData() async {
    final deptID = await LocalStorage.getstr('deptId');
    try {
      final response = await _networkService.get(
          '/plcTimer/malfunction/malfunctionInfo?companyId=1&malType=$deptID');
      if (response.statusCode == 200) {
        return response.data;
      } else {
        print('Failed to load data: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      print('Error in _fetchData: $e');
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('故障详情'),
        backgroundColor: Colors.transparent,
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/home/home_topbg.png'),
              fit: BoxFit.cover,
            ),
          ),
        ),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 列表
          Expanded(
            child: ListView.builder(
              itemCount: listData.length,
              shrinkWrap: true, // 适应内容高度
              physics: const NeverScrollableScrollPhysics(), // 禁止滚动
              itemBuilder: (context, index) {
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 0.0), // 设置行间距为1
                  child: GestureDetector(
                    // 添加点击事件
                    onTap: () {},
                    child: BreakdownItem(data: listData[index]),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
